package com.wanggsx.library.base.views.radiobutton;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wanggsx.library.R;
import com.wanggsx.library.util.UtilsSize;

import java.util.List;

/**
 * Created by wanggsx on 15/12/10.
 */
public class MyRadioButton extends LinearLayout implements View.OnClickListener {

    private OnSelectChangeListener mListener;
    private List mTexts;
    private LinearLayout mllytContainer;
    private ScrollViewForRadioButton mSlideBlock;
    private Context mContext;
    private int currentIndex = 0;
    private int mTextSize, mMarginLeftRight;
    private int mFirstScrollPos = 0;
    //定义每一个按钮的宽度dp
    private int WIDTH_BUTTON = 44;
    //定义滑动块的宽度dp
    private int WIDTH_SLIDER = 32;
    //定义滑动块距离文字的高度dp
    private int HEIGHT_TO_TOP_SLIDER = 3;
    //定义滑动块距离底部的高度dp
    private int HEIGHT_TO_BOTTOM_SLIDER = 12;
    //定义滑动块的高度DP
    private int HEIGHT_SLIDER = 3;
    private int mWidth = 0;
    private boolean isInitFinish = false;
    private int initPostion = 0;

    public MyRadioButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        setOrientation(VERTICAL);
        setGravity(Gravity.CENTER);
        mllytContainer = new LinearLayout(context);
        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, 0, 1);
        mllytContainer.setLayoutParams(params);
        mllytContainer.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
        addView(mllytContainer);
        // dp 转 px
        WIDTH_BUTTON = UtilsSize.dp2px(WIDTH_BUTTON);
        WIDTH_SLIDER = UtilsSize.dp2px(WIDTH_SLIDER);
        HEIGHT_SLIDER = UtilsSize.dp2px(HEIGHT_SLIDER);
        HEIGHT_TO_BOTTOM_SLIDER = UtilsSize.dp2px(HEIGHT_TO_BOTTOM_SLIDER);
    }

    public void initButtons(List buttonTexts, int textSize, int marginLeftRight, OnSelectChangeListener l) {
        mListener = l;
        mTexts = buttonTexts;
        mMarginLeftRight = marginLeftRight;
        mllytContainer.removeAllViews();
        if (buttonTexts != null && buttonTexts.size() >= 1) {
            for (int i = 0; i < buttonTexts.size(); i++) {
                TextView btn = new TextView(mContext);
                LayoutParams params = new LayoutParams(WIDTH_BUTTON, LayoutParams.WRAP_CONTENT);
                params.setMargins(marginLeftRight, 0, marginLeftRight, 0);
                btn.setLayoutParams(params);
                btn.setGravity(Gravity.CENTER);
                btn.setTextSize(textSize);
                btn.setTextColor(Color.WHITE);
                btn.setText((String) buttonTexts.get(i));
                btn.setClickable(true);
                btn.setOnClickListener(this);
                btn.setTag(i);
                mllytContainer.addView(btn);
            }
        }
        mSlideBlock = new ScrollViewForRadioButton(mContext);
        LayoutParams paramsBlock = new LayoutParams(LayoutParams.MATCH_PARENT, HEIGHT_SLIDER);
        paramsBlock.setMargins(0, HEIGHT_TO_TOP_SLIDER, 0, HEIGHT_TO_BOTTOM_SLIDER);
        mSlideBlock.setLayoutParams(paramsBlock);
        mSlideBlock.setHorizontalScrollBarEnabled(false);
        addView(mSlideBlock);
        LinearLayout layout = new LinearLayout(mContext);
        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        layout.setLayoutParams(params);
        layout.setOrientation(HORIZONTAL);
        mSlideBlock.addView(layout);
    }

    public void setCurrentIndex(int index) {
        if (((LinearLayout) mSlideBlock.getChildAt(0)).getChildCount() == 0) {
            initPostion = index;
        } else {
            TextView tv = (TextView) mllytContainer.getChildAt(index);
            if (tv != null) {
                initPostion = 0;
                tv.performClick();
            }
        }
    }

    public void setCurrentText(String text) {
        if (text == null) return;
        for (int i = 0; i < mTexts.size(); i++) {
            if (text.equals(mTexts.get(i))) {
                setCurrentIndex(i);
            }
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        //if (!isInitFinish) {
        isInitFinish = true;
        ((LinearLayout) mSlideBlock.getChildAt(0)).removeAllViews();
        //添加滑块内左边距
        View view = new View(mContext);
        LayoutParams params = new LayoutParams(mWidth, ViewGroup.LayoutParams.MATCH_PARENT);
        view.setLayoutParams(params);
        view.setBackgroundColor(0x00000000);
        ((LinearLayout) mSlideBlock.getChildAt(0)).addView(view);
        //添加滑动内右边距
        View view1 = new View(mContext);
        LayoutParams params1 = new LayoutParams(mWidth, ViewGroup.LayoutParams.MATCH_PARENT);
        view1.setLayoutParams(params1);
        view1.setBackgroundColor(0x00000000);
        ((LinearLayout) mSlideBlock.getChildAt(0)).addView(view1);
        //添加滑块
        View mSlideLine = new View(mContext);
        LayoutParams paramLine = new LayoutParams(WIDTH_SLIDER, HEIGHT_SLIDER);
        mSlideLine.setLayoutParams(paramLine);
        mSlideLine.setBackgroundResource(R.drawable.shape_round_white_line);
        ((LinearLayout) mSlideBlock.getChildAt(0)).addView(mSlideLine, 1);
        scrollSlide(100);
        //}
        if (initPostion >= 1) {
            setCurrentIndex(initPostion);
            initPostion = 0;
        }
    }

    private void scrollSlide(int addMillSecondDelay) {
        mSlideBlock.postDelayed(new Runnable() {
            @Override
            public void run() {
                int dis = mWidth - ((mWidth - mllytContainer.getWidth()) / 2 + mllytContainer.getChildAt(0).getWidth() / 2 + currentIndex * (mMarginLeftRight * 2 + WIDTH_BUTTON));
                mSlideBlock.smoothScrollTo(dis, 0);
            }
        }, 100 + addMillSecondDelay);
    }

    @Override
    public void onClick(View v) {
        if (v instanceof TextView) {
            mListener.onChanged(((TextView) v).getText().toString());
            currentIndex = (int) v.getTag();
            scrollSlide(0);
        }
    }
}
